package waittime;

import socketing.Tool;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Consumer;

public class AxWait {
//    public static void main1(String[] args) {
//
//        String lock ="lock";
//
//        while (true){
//            synchronized (lock){
//                try {
//                    lock.wait(2000);
//                } catch (InterruptedException e) {
//                    System.out.println("exexe")}
//            }
//
//            System.out.println("hello");
//        }
//
//    }
    public static final ExecutorService ajaxes = Executors.newFixedThreadPool(3);

    public static final BlockingQueue<Object> queue = new LinkedBlockingQueue<Object>(10);
    public static void main(String[] args) {

        new Thread(()->  {

                debug("开始执行js文件　var a =4;");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                debug("执行完毕js文件　function over");
                final BlockingQueue<Object> eventQueue = queue;

                while (true){

                    try {
                        Object c = queue.take();
                        if (c instanceof ClickTask){
                            ClickTask c1 = (ClickTask) c;
                            c1.getOnclick().accept(c1.getXy());

                        }else if(c instanceof AjaxTask){
                            AjaxTask c2= (AjaxTask) c;
                            c2.getSuccess().accept(c2.getRes());

                        }else {
                            System.out.println("不认识的类型　pass");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            },"js 主线程").start();
        new Thread(()->{
            final BlockingQueue<Object> eQueue = queue;
            Scanner scanner = new Scanner(System.in);
            debug("就绪");
            while (true){
                String s = scanner.nextLine();
                 debug("发送并接受到回复"+s);
                int i;
                 try {
                     i = Integer.parseInt(s);

                 }catch (Exception e){
                     i=1000;
                 }
                ClickTask clickTask = new ClickTask(i,(e)->{
                    debug("???onclick回调函数执行中　，将要发送请求");
                    String request = "localhost:8080/path?get";
                    Consumer<String> success= (res) -> {
                        debug("???ajax一步请求回调函数执行中" + res);};
                    ajax(request,success);
                });
                try {
                    queue.put(clickTask);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


            }
        },"用户操作由操作系统完成").start();

        debug("end");


    }

    public static void debug(Object o){
        String s = "Thread : "+Thread.currentThread().getName();

        System.out.println(s+" o: "+o.toString());
    }
    public static void ajax(String obj,Consumer<String> success){
        ajaxes.submit(()->{
//            使用V2server模拟
            try {
                Socket socket = new Socket("master", 4444);
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(
                                socket.getOutputStream(), StandardCharsets.UTF_8));
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(
                                socket.getInputStream(), StandardCharsets.UTF_8));
                debug("to send and wait res "+obj);
                writer.write(obj);
                writer.newLine();
                writer.flush();
                String res = reader.readLine();
                debug("after waiting got "+res);
                reader.close();
                writer.close();
                socket.close();

                AjaxTask ajaxTask = new AjaxTask(res, success);
                queue.put(ajaxTask);


            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
class ClickTask{
    private int xy;
    private Consumer<Integer> onclick;

    public ClickTask(int xy, Consumer<Integer> onclick) {
        this.xy = xy;
        this.onclick = onclick;
    }

    public int getXy() {
        return xy;
    }

    public void setXy(int xy) {
        this.xy = xy;
    }

    public Consumer<Integer> getOnclick() {
        return onclick;
    }

    public void setOnclick(Consumer<Integer> onclick) {
        this.onclick = onclick;
    }
}
class AjaxTask{
    private String res;

    private Consumer<String> success;

    public AjaxTask(String res, Consumer<String> success) {
        this.res = res;
        this.success = success;
    }

    public String getRes() {
        return res;
    }

    public void setRes(String res) {
        this.res = res;
    }

    public Consumer<String> getSuccess() {
        return success;
    }

    public void setSuccess(Consumer<String> success) {
        this.success = success;
    }
}
